﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;
using Common.DbHelper;
namespace DAL
{
    public partial class AccountDAL
    {
        public Admin GetLogin(string userName, string userPwd)
        {
            string strSql = string.Format(@"SELECT * FROM Admin WHERE UserName='{0}' AND Password='{1}' ", userName, userPwd);
            DataSet ds = DbHelperMySQL.Query(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Admin DataRowToModel(DataRow row)
        {
            Admin model = new Admin();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["UserName"] != null)
                {
                    model.UserName = row["UserName"].ToString();
                }
                if (row["Password"] != null)
                {
                    model.Password = row["Password"].ToString();
                }
                if (row["Purview"] != null && row["Purview"].ToString() != "")
                {
                    model.Purview = int.Parse(row["Purview"].ToString());
                }
                if (row["LastLoginIP"] != null && row["LastLoginIP"].ToString() != "")
                {
                    model.LastLoginIP = row["LastLoginIP"].ToString();
                }
                if (row["LastLoginTime"] != null)
                {
                    model.LastLoginTime =Convert.ToDateTime(row["LastLoginTime"].ToString());
                }
                if (row["LastLogoutTime"] != null && row["LastLogoutTime"].ToString() != "")
                {
                    model.LastLogoutTime = Convert.ToDateTime(row["LastLogoutTime"].ToString());
                }
                if (row["LoginTimes"] != null && row["LoginTimes"].ToString() != "")
                {
                    model.LoginTimes = int.Parse(row["LoginTimes"].ToString());
                }

                if (row["AdminPurview_Article"] != null && row["AdminPurview_Article"].ToString() != "")
                {
                    model.Purview = int.Parse(row["AdminPurview_Article"].ToString());
                }
                if (row["AdminPurview_Soft"] != null && row["AdminPurview_Soft"].ToString() != "")
                {
                    model.AdminPurview_Soft =int.Parse(row["AdminPurview_Soft"].ToString());
                }
                if (row["AdminPurview_Photo"] != null)
                {
                    model.AdminPurview_Photo = int.Parse(row["AdminPurview_Photo"].ToString());
                }
                if (row["AdminPurview_Guest"] != null && row["AdminPurview_Guest"].ToString() != "")
                {
                    model.AdminPurview_Guest =row["AdminPurview_Guest"].ToString();
                }
                if (row["AdminPurview_Others"] != null && row["AdminPurview_Others"].ToString() != "")
                {
                    model.AdminPurview_Others = row["AdminPurview_Others"].ToString();
                }
                if (row["RndPassword"] != null && row["RndPassword"].ToString() != "")
                {
                    model.RndPassword = row["RndPassword"].ToString();
                }
                
            }
            return model;
        }
    }
}
